home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Beziers and Other Splines / BezierCircles / BezierCircles.cs next >
Encoding:
Text File  |  2001-01-15  |  2.7 KB  |  63 lines

  1. //--------------------------------------------
  2. // BezierCircles.cs ⌐ 2001 by Charles Petzold
  3. //--------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class BezierCircles: PrintableForm
  9. {
  10.      public new static void Main()
  11.      { 
  12.           Application.Run(new BezierCircles());
  13.      }
  14.      public BezierCircles()
  15.      {
  16.           Text = "Bezier Circles";
  17.      }
  18.      protected override void DoPage(Graphics grfx, Color clr, int cx, int cy)
  19.      {
  20.           int iRadius = Math.Min(cx - 1, cy - 1) / 2;
  21.  
  22.           grfx.DrawEllipse(new Pen(clr), cx / 2 - iRadius, cy / 2 - iRadius, 
  23.                                          2 * iRadius, 2 * iRadius);
  24.  
  25.                // Two-segment (180-degree) approximation
  26.  
  27.           int L = (int) Math.Round(iRadius * 4f / 3 * Math.Tan(Math.PI / 4));
  28.  
  29.           Point[] apt = {
  30.                               new Point(cx / 2,     cy / 2 - iRadius),
  31.                               new Point(cx / 2 + L, cy / 2 - iRadius),
  32.                               new Point(cx / 2 + L, cy / 2 + iRadius), 
  33.                               new Point(cx / 2,     cy / 2 + iRadius),
  34.                               new Point(cx / 2 - L, cy / 2 + iRadius),
  35.                               new Point(cx / 2 - L, cy / 2 - iRadius),
  36.                               new Point(cx / 2,     cy / 2 - iRadius)
  37.                         };
  38.           grfx.DrawBeziers(Pens.Blue, apt);
  39.  
  40.                // Four-segment (90-degree) approximation
  41.  
  42.           L = (int) Math.Round(iRadius * 4f / 3 * Math.Tan(Math.PI / 8));
  43.  
  44.           apt = new Point[] 
  45.                          {
  46.                               new Point(cx / 2,           cy / 2 - iRadius),
  47.                               new Point(cx / 2 + L,       cy / 2 - iRadius),
  48.                               new Point(cx / 2 + iRadius, cy / 2 - L),
  49.                               new Point(cx / 2 + iRadius, cy / 2),
  50.                               new Point(cx / 2 + iRadius, cy / 2 + L),
  51.                               new Point(cx / 2 + L,       cy / 2 + iRadius), 
  52.                               new Point(cx / 2,           cy / 2 + iRadius),
  53.                               new Point(cx / 2 - L,       cy / 2 + iRadius),
  54.                               new Point(cx / 2 - iRadius, cy / 2 + L),
  55.                               new Point(cx / 2 - iRadius, cy / 2),
  56.                               new Point(cx / 2 - iRadius, cy / 2 - L),
  57.                               new Point(cx / 2 - L,       cy / 2 - iRadius),
  58.                               new Point(cx / 2,           cy / 2 - iRadius)
  59.                          };
  60.           grfx.DrawBeziers(Pens.Red, apt);
  61.      }
  62. }
  63.